package com.zhang.pdf.utils;

import cn.hutool.core.date.DateUtil;
import net.sourceforge.tess4j.TesseractException;

import com.itextpdf.text.DocumentException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


@Component
public class PDFreder {
    /**
     * PDF文件目录
     */
    private final static String PDF_PATH = "D:/pdf/";
    /**
     * 输出excel目录
     */
    private final static String EXCEL_PATH = "D:/excel/";
    
    @Scheduled(cron="0/5 * *  * * ? ")   //每5秒执行一次
    public void execute(){
        File begin = new File(PDF_PATH+"1.txt");
        if(!begin.exists()){
            return;
        }
        List<String> folderMethod = folderMethod(PDF_PATH);
        try {
            if(folderMethod.size() > 0){
                getPdfFileText(folderMethod,EXCEL_PATH);
            }
        } catch (IOException | DocumentException | TesseractException e) {
            e.printStackTrace();
        }
        //执行完之后删除所有pdf文件
        File file = new File(PDF_PATH);
        deleteFile(file);
    }
    /**
     * 删除目录下所有文件
     * @param file
     */
    private static void deleteFile(File file){
        //取得这个目录下的所有子文件对象
        File[] files = file.listFiles();
        //遍历该目录下的文件对象
        for (File f: files){
            f.delete();
        }
    }
    
	public void getPdfFileText(List<String> StrFiles, String path) throws IOException, DocumentException, TesseractException {
		List<ExcelModel> dataList = new ArrayList<>();
		for (int p = 0; p < StrFiles.size(); p++) {
			String str = StrFiles.get(p);
			System.out.println("正在解析："+str);
			List<ExcelModel> list = ReadPDF.readPdfNoImg(str);
			dataList.addAll(list);
		}
		if(dataList != null && dataList.size() > 0){
	        String javaBeanname = "orderNo,name,phone,expressNo";
	        String thname = "订单号,姓名,手机号,快递单号";
	        String fileName = DateUtil.format(new Date(), "yyyy-MM-dd-HH-mm-ss") + ".xls";
	        ExportExcelPdf.createExcel(thname, javaBeanname, dataList, path, fileName);
		}
        System.out.println("解析完成！");
	}

	public List<String> folderMethod(String path) {
		File file = new File(path);
		LinkedList<File> list = new LinkedList<>();
		LinkedList<File> pdfList = new LinkedList<>();
		if (file.exists()) {
			if (null == file.listFiles()) {
				if (file.getAbsolutePath().endsWith(".pdf"))
					pdfList.add(file);
			} else {
				list.addAll(Arrays.asList(file.listFiles()));
				while (!list.isEmpty()) {
					File firstF = list.removeFirst();
					if (firstF.getAbsolutePath().endsWith(".pdf"))
						pdfList.add(firstF);
					File[] files = firstF.listFiles();
					if (null == files)
						continue;
					for (File f : files) {
						if (f.isDirectory()) {
							list.add(f);
						} else if (f.getAbsolutePath().endsWith(".pdf")) {
							pdfList.add(f);
						}
					}
				}
			}
		} else {
			System.out.println("文件不存在");
		}
		List<String> StrFiles = new ArrayList<>();
		for (File f : pdfList) {
			StrFiles.add(f.getAbsolutePath());
		}
		return StrFiles;
	}
}
